package com.lankystar.logistucs.spring.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.logistucs.spring.dao.ICargoDao;
import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.CargoInfo;

@Repository
public class CargoDaoImpl extends BaseDaoImpl implements ICargoDao {

	@Autowired
	private SessionFactory sf;
	
	public Session getSession(){
		System.out.println("session  cargoDaoImpl!!");
		return sf.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CargoInfo> selectCargoInfolist(int userId) {
		String sql = "select ci from CargoInfo ci join ci.user u where u.userId = ? and ci.cargo = "+null+"";
		List<CargoInfo> cargoInfolist = getSession().createQuery(sql).setInteger(0, userId).list();
		return cargoInfolist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectCargolist(int userId) {
		String sql = "from Cargo c join c.user u join c.address where u.userId = ? and c.oneOrder = "+null+"";
		List<Object[]> cargolist = getSession().createQuery(sql).setInteger(0, userId).list();
		return cargolist;
	}

	@Override
	public CargoInfo selectCargoInfoById(int infoId) {
		CargoInfo cargoInfo = (CargoInfo) getSession().get(CargoInfo.class, infoId);
		return cargoInfo;
	}

	@Override
	public Cargo selectCargoById(int cargoId) {
		Cargo cargo = (Cargo) getSession().get(Cargo.class, cargoId);
		return cargo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CargoInfo> selectCargoInfoBycargoId(int cargoId) {
		List<CargoInfo> cargoInfolist = getSession().createQuery("select ci from CargoInfo ci join ci.cargo c where c.cargoId = ?")
													.setInteger(0, cargoId).list();
		return cargoInfolist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectCargoAndAddresslist() {
		String sql = "from Cargo c join c.user u join c.address where c.oneOrder = "+null+" and c.broker != "+null+"";
		List<Object[]> cargolist = getSession().createQuery(sql).list();
		return cargolist;
	}

	@Override
	public Cargo selectCargoByTotalOrderNu(String totalOrderNu) {
		String hql = "from Cargo c where c.totalOrderNu = ? and c.broker = "+null+"";
		Cargo cargo = (Cargo) getSession().createQuery(hql).setParameter(0, totalOrderNu).uniqueResult();
		return cargo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectLastOrderAndAddresslist() {
		String sql = "from Cargo c join c.user u join c.address where c.oneOrder = "+null+" and c.broker = "+null+"";
		List<Object[]> cargolist = getSession().createQuery(sql).list();
		return cargolist;
	}

	@Override
	public Object[] selectCargoAndAddressBycargoId(int cargoId) {
		String hql = "from Cargo c join c.address a where c.cargoId = ?";
		Object[] ob = (Object[]) getSession().createQuery(hql).setInteger(0, cargoId).uniqueResult();
		return ob;
	}
}
